<?php


namespace Biz\model\advertisement;


class MPositionAdRuleDetail extends MBaseDbModel
{
    const _CONFIG_FILE_ = 'db_position';

    const _CONFIG_SELECT_ = 'db_position';

    const _DATABASE_ = 'hhz_position';

    const _TABLE_NAME = 'position_ad_rule_detail';

    const IS_DELETED_YES = 1;
    const IS_DELETED_NO = 0;

    public static function edit($adRuleId, $adTypeIds)
    {
        if (!$adRuleId) {
            return;
        }

        $details = self::select('ad_type_id', ['ad_rule_id' => $adRuleId, 'is_deleted' => self::IS_DELETED_NO]);

        $needToInsert = array_diff($adTypeIds, $details);
        $needToDelete = array_diff($details, $adTypeIds);
        $insertData = [];
        foreach ($needToInsert as $datum) {
            $insertData[] = [
                'ad_rule_id' => $adRuleId,
                'ad_type_id' => $datum,
                'is_deleted' => self::IS_DELETED_NO,
            ];
        }
        if ($insertData) {
            self::insert($insertData);
        }
        if ($needToDelete) {
            $where = [
                'ad_rule_id' => $adRuleId,
                'ad_type_id' => array_values($needToDelete),
            ];
            self::update(['is_deleted' => self::IS_DELETED_YES], $where);
        }
    }

}
